// 有关账号和验证码逻辑的封装
import { validEmail } from "@/utils/validate";
import captcha from "@/api/us/captcha";
import user from "@/api/us/user"

export const account = {
  data() {
    return {
      // 是否注册过
      isRegistered: false,
      // 是否通过手机/邮箱验证
      checked: false
    }
  },
  methods: {
    isEmailorSms(account) {
      if (validEmail(account)) {
        return true
      } else {
        return false
      }
    },
    // 验证手机验证码
    async checkSmsCaptcha(account, code) {
      try {
        const res = await captcha.checkSmsCaptcha(account, code)
        this.checked = true
      } catch (err) {
        this.loading = false
      }
    },
    // 电子邮箱验证
    async checkEmailCaptcha(account, code) {
      const res = await captcha.checkEmailCaptcha(account, code);
      if (res.code === 200) {
        this.checked = true
      }
    },
    // 是否注册过
    async checkAccount(account) {
      const {
        isRegistered
      } = await user.checkUserIsRegistered(account);
      this.isRegistered = isRegistered
      return (isRegistered)
    }
  }
}
